//
//  QXNetWorkTool.swift
//  DanTangSwift
//
//  Created by 众力金融 on 2017/6/19.
//  Copyright © 2017年 ..... All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

enum MethodType: String {
    case get  = "GET"
    case post = "POST"
}

class NetWorkTool: NSObject {
    
    static let defManager: SessionManager = {
        var defHeaders = Alamofire.SessionManager.default.session.configuration.httpAdditionalHeaders
        let defConf = URLSessionConfiguration.default
        defConf.timeoutIntervalForRequest = 8
        defConf.httpAdditionalHeaders = defHeaders
        return Alamofire.SessionManager(configuration: defConf)
    }()
    
}

extension NetWorkTool {
    
  static  func requestData(type: MethodType, urlString: String, parameters: [String : Any]? = nil ,succeed:@escaping (_ result : Any) -> () ,failure:@escaping (_ error: Error) -> ()) {
        
        //1.获取类型
        let metodty = type == .get ? HTTPMethod.get : HTTPMethod.post
        //2.发起网络数据请求
        NetWorkTool.defManager.request(urlString, method: metodty, parameters: parameters).responseJSON { (response) in
            
            //请求失败
            if response.result.isFailure {
                failure(response.result.error!)
                return
            }
            
            if response.result.isSuccess {
                
                guard let result = response.result.value else {
                    
                    failure(response.result.error!)
                    return
                }
                
                succeed(result)
                
            }
            
            
        }
        
    }
    
}


